import {
  Injectable,
  ViewContainerRef,
  ComponentFactoryResolver,
  ComponentFactory
} from "@angular/core";
import { FormGroup, FormBuilder, Validators, FormArray } from "@angular/forms";
import { CreateModalComponent } from "./create-modal/create-modal.component";
import { Iwe7UrlService } from "../iwe7-tools/iwe7-url.service";
import { HttpClient } from "@angular/common/http";
@Injectable()
export class Iwe7FormBuilderService {
  form: FormGroup;
  params: FormArray;
  fields: FormArray;

  constructor(
    public fb: FormBuilder,
    public url: Iwe7UrlService,
    public http: HttpClient
  ) {
    this.form = this.fb.group({
      title: ["", Validators.required],
      // 链接
      url: [""],
      table: [""],
      btn: [""],
      // 动态参数
      params: this.fb.array([]),
      // 表单字段
      fields: this.fb.array([])
    });
    this.params = this.form.get("params") as FormArray;
    this.fields = this.form.get("fields") as FormArray;

    this.form.valueChanges.subscribe(res => {
      // console.log(res);
    });

    this.fields.valueChanges.subscribe(res=>{
      // console.log(res);
    });
  }
  // 添加动态参数
  addParams() {
    this.params.push(
      this.fb.group({
        name: ["", Validators.required],
        form: ["", Validators.required]
      })
    );
  }
  // 添加表单字段
  addFields() {
    this.fields.push(
      this.fb.group({
        title: ["", Validators.required],
        name: ["", Validators.required],
        type: ["text", Validators.required],
        placeholder: ["", Validators.required],
        required: false,
        empty: ""
      })
    );
  }

  deleteField(index: number) {
    this.fields.removeAt(index);
  }

  deleteParams(index: number) {
    this.params.removeAt(index);
  }

  // 添加一个form
  moduleRef: any;
  addForm(currentIndex: number) {}
  // 编辑一个form
  editForm(form: any) {
    this.form.patchValue(form);
    let field = this.fb.group({
      title: ["", Validators.required],
      name: ["", Validators.required],
      type: ["text", Validators.required],
      placeholder: ["", Validators.required],
      required: false,
      empty: ""
    });
    let params = this.fb.group({
      name: ["", Validators.required],
      form: ["", Validators.required]
    });
    form.fields.map(f => {
      field.patchValue(f);
      this.fields.push(field);
    });
    form.params.map(p => {
      params.patchValue(p);
      this.params.push(params);
    });
  }
  // 删除一个form
  deleteForm(id: number) {
    let url = this.url.getOpenUrl("formBuilder/deleteForm");
    return this.http.post(url, {
      id: id
    });
  }

  getList() {
    let url = this.url.getOpenUrl("formBuilder/getList");
    return this.http.get(url);
  }

  getData(id: any) {
    let url = this.url.getOpenUrl("formBuilder/getData", { id: id });
    return this.http.get(url);
  }

  saveData(id: string, data: any) {
    let url = this.url.getOpenUrl("formBuilder/saveData", { id: id });
    return this.http.post(url, data);
  }

  save(id?: number) {
    let url = this.url.getOpenUrl("formBuilder/save", { id: id });
    return this.http.post(url, this.form.value);
  }

  getDetail(id: any) {
    let url = this.url.getOpenUrl("formBuilder/getDetail", { id: id });
    return this.http.get(url);
  }
}
